package com.xxx.video.login.presenters;

import android.content.Context;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.login.beans.UserSecretGuardBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

import java.util.List;


public class ForgetPwdFirstPresenter extends BasePresenter<IForgetPwdFirstView> {
    private int size;

    public ForgetPwdFirstPresenter(@NonNull Context context, IForgetPwdFirstView baseView) {
        super(context, baseView);
    }

    /**
     * get user secret Guard
     *
     * @param userName user name
     */
    public void getSecretGuard(String userName) {
        NetWorkUtils.getInstance().getSecretGuard(userName, new CommonResponseListener<UserSecretGuardBean>() {
            @Override
            public void onSuccess(UserSecretGuardBean successOb, String result) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        size = successOb.list.size();
                        getBaseView().initSecretGuardData2View(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                if (getBaseView() != null) {
                    getBaseView().showRootLayoutStatus(1);
                }
            }
        });

    }

    /**
     * start forget pwd second activity
     */
    public void startForgetPwdSecondActivity() {
        List<UserSecretGuardBean.ListBean> mUserSecretGuardBeanList = getBaseView() == null ? null : getBaseView().getUserSecretGuardList();
        String firstAnswer = getBaseView() == null ? "" : getBaseView().getFirstQuestion();
        String secondAnswer = getBaseView() == null ? "" : getBaseView().getSecondQuestion();
        String thirdAnswer = getBaseView() == null ? "" : getBaseView().getThirdQuestion();
        if (ListUtils.isEmpty(mUserSecretGuardBeanList)) {
            return;
        }
        if (size == 1) {
            if (TextUtils.isEmpty(firstAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_first_answer_can_not_empty));
                return;
            }
            if (!TextUtils.equals(firstAnswer, mUserSecretGuardBeanList.get(0).fAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_first_answer_can_not_right));
                return;
            }
        } else if (size == 2) {
            if (TextUtils.isEmpty(firstAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_first_answer_can_not_empty));
                return;
            }
            if (TextUtils.isEmpty(secondAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_second_answer_can_not_empty));
                return;
            }
            if (!TextUtils.equals(firstAnswer, mUserSecretGuardBeanList.get(0).fAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_first_answer_can_not_right));
                return;
            }
            if (!TextUtils.equals(secondAnswer, mUserSecretGuardBeanList.get(1).fAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_second_answer_can_not_right));
                return;
            }
        } else {
            if (TextUtils.isEmpty(firstAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_first_answer_can_not_empty));
                return;
            }
            if (TextUtils.isEmpty(secondAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_second_answer_can_not_empty));
                return;
            }
            if (TextUtils.isEmpty(thirdAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_third_answer_can_not_empty));
                return;
            }
            if (!TextUtils.equals(firstAnswer, mUserSecretGuardBeanList.get(0).fAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_first_answer_can_not_right));
                return;
            }
            if (!TextUtils.equals(secondAnswer, mUserSecretGuardBeanList.get(1).fAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_second_answer_can_not_right));
                return;
            }
            if (!TextUtils.equals(thirdAnswer, mUserSecretGuardBeanList.get(2).fAnswer)) {
                ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_forget_pwd_first_third_answer_can_not_right));
                return;
            }

        }

        if (getBaseView() != null) {
            getBaseView().startForgetPwdSecondActivity();
        }
    }
}
